package com.ycfmedical.bed.ui.fragment;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.adapter.HealthFoodAdapter;
import com.ycfmedical.bed.adapter.TimerReminderAdapter;
import com.ycfmedical.bed.local.DataManager;
import com.ycfmedical.bed.local.entity.TimerReminder;
import com.ycfmedical.bed.model.HealthResults;
import com.ycfmedical.bed.present.TimerReminderPresenter;
import com.ycfmedical.bed.ui.HealthDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XFragment;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;

/**
 * Created by Administrator on 2017-04-13.
 */

public class TimerReminderFragment extends XFragment<TimerReminderPresenter> {
    @BindView(R.id.timing_list)
    XRecyclerContentLayout mContentLayout;
    @BindView(R.id.new_timing)
    ImageButton newTiming;
    TimerReminderAdapter adapter;
    // 设置成功时提醒的String
    String stWarn;

    @Override
    public void initData(Bundle savedInstanceState)
    {
        _initAdapter();
        mContentLayout.getRecyclerView().setRefreshEnabled(false);
        mContentLayout.getRecyclerView().verticalLayoutManager(getContext());
        mContentLayout.getRecyclerView().setAdapter(adapter);
    }

    private void _initAdapter()
    {
        adapter = new TimerReminderAdapter(context);

        adapter.setRecItemClick(new RecyclerItemCallback<TimerReminder, TimerReminderAdapter.ViewHolder>()
        {
            @Override
            public void onItemClick(int position, TimerReminder model, int tag, TimerReminderAdapter.ViewHolder holder)
            {
                super.onItemClick(position, model, tag, holder);
                switch (tag)
                {
                    case TimerReminderAdapter.TAG_DELETE:
                        //清除对应adapter
                        try
                        {
                            adapter.removeElement(position);
                            //更新list
                            DataManager.getInstance().getmTimerList().remove(position);
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        //更新界面
                        adapter.notifyDataSetChanged();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_timingreminder;
    }

    @Override
    public TimerReminderPresenter newP()
    {
        return new TimerReminderPresenter(context);
    }

    @OnClick(R.id.new_timing)
    public void onViewClicked()
    {
        getP().TimePicker();
    }

    public void showData()
    {
        List<TimerReminder> timerReminders = DataManager.getInstance().getmTimerList();
        adapter.clearData();
        adapter.addData(timerReminders);
        adapter.notifyDataSetChanged();
    }

    public void showToast(String msg) {
        stWarn = msg;
    }
}
